package com.qf.interceptor;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.User;
import com.qf.util.ResultMsg;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

public class LoginInterceptor implements HandlerInterceptor {
    //在请求操作执行之前
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o) throws Exception {
        //order页面--》拦截到登录页面
        //order订单相关操作-->权限验证
        HttpSession session = httpServletRequest.getSession();
        Object obj=session.getAttribute("sys_user");
//        if(obj instanceof User){
//            User user=(User) obj;
//            return true;//放行
//        }
//        //??? login.html  同步/异步 ResultMsg--code--400未登录
//        //异步反回
//        ResultMsg resultMsg=new ResultMsg();
//        resultMsg.setCode(400);
//        response.setContentType("text/json");//将响应结果改为json
//        PrintWriter pw = response.getWriter();
//        pw.write(JSON.toJSONString(resultMsg));
//        pw.flush();
//        pw.close();
        return true;//true放行/false拦截
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
